const { queryDatabase } = require('../queue-client');

// 统计信息
async function getStatistics() {
  try {
    const sql = `
      SELECT 
        COUNT(*) as total_slots,
        SUM(CASE WHEN has_goods = 1 THEN 1 ELSE 0 END) as occupied_slots,
        SUM(CASE WHEN has_goods = 0 THEN 1 ELSE 0 END) as empty_slots
      FROM storage_slots
    `;
    
    const rows = await queryDatabase(sql);
    return rows[0];
  } catch (error) {
    console.error('获取统计信息失败:', error);
    throw error;
  }
}

// 按层级统计
async function getLayerStatistics() {
  try {
    const sql = `
      SELECT 
        layer,
        COUNT(*) as total,
        SUM(CASE WHEN has_goods = 1 THEN 1 ELSE 0 END) as occupied,
        SUM(CASE WHEN has_goods = 0 THEN 1 ELSE 0 END) as empty
      FROM storage_slots
      GROUP BY layer
      ORDER BY layer
    `;
    
    const rows = await queryDatabase(sql);
    return rows;
  } catch (error) {
    console.error('获取层级统计信息失败:', error);
    throw error;
  }
}

module.exports = {
  getStatistics,
  getLayerStatistics
};